const callingList = []
const dhWeb = new DHAlarmWeb()
const dhConfig = {}
let isHanding = false
const NotifyUsWeb = function () {
  this.onNotifyAlarm = null
  this.onStopMusic = null
  this.onPlayMusic = null
}
const notifyWeb = new NotifyUsWeb()

export function getDhWeb() {
  return dhWeb
}

export function getNotifyWeb() {
  return notifyWeb
}

export function initDiiHoo(dhUsername, dhPassword, dhServerIP, dhServerPort) {
  dhConfig.dhUsername = dhUsername
  dhConfig.dhPassword = dhPassword
  dhConfig.dhServerIP = dhServerIP
  dhConfig.dhServerPort = dhServerPort
  dhWeb.setWebsocketPort({dataWsPort: dhConfig.dhServerPort, mediaWsPort: dhConfig.dhServerPort});
  login()
  dhWeb.onLogin = onLogin
  dhWeb.onAlarmServerClosed = onAlarmServerClosed
  dhWeb.onPlayRT = onPlayRT
  dhWeb.onNotify = onNotify
  dhWeb.onDeviceList = onDeviceList
  dhWeb.onDeviceVideoData = onDeviceVideoData
}

export function login() {
  dhWeb.login(dhConfig.dhUsername, dhConfig.dhPassword, dhConfig.dhServerIP);
}

export function logout() {
  dhWeb.logout(sessionStorage.getItem('loginHandle'));
  sessionStorage.setItem('loginHandle', null);
  sessionStorage.setItem('isLogin', false);
}

export function onLogin(data) {
  dhWeb.isLogin = true
  let params = data.params;
  if (data.error == "success") {
    sessionStorage.setItem('loginHandle', params.loginHandle);
    sessionStorage.setItem('isLogin', true);
  } else if (data.error === "repeat") {
    if (sessionStorage.setItem('isLogin') != "true") {
      return;
    }
  } else if (data.error === "authfail") {
    this.$message({message: '账号或密码错误', type: 'error'});
  } else {
    setTimeout(this.login, 2000)
    this.$message({message: '登录失败', type: 'error'});
  }
}

export function onAlarmServerClosed() {
  if (sessionStorage.getItem('isLogin') === "true") {
    setTimeout(function () {
      login();
    }, 5000)
  }
}

export function pushCallList(callInfo) {
  removeCallList(callInfo.deviceId)
  callingList.push(callInfo)
  notifyWebUs()
}

export function removeCallList(deviceId) {
  callingList.forEach(function (item, index, arr) {
    if (item.deviceId == deviceId) {
      arr.splice(index, 1);
      notifyWebUs()
    }
  });
}

export function notifyWebUs() {
  if (notifyWeb.onNotifyAlarm) {
    notifyWeb.onNotifyAlarm(callingList)
  }
}

export function stopRT(deviceId) {
  this.dhWeb.stopRT(deviceId, sessionStorage.getItem('loginHandle'));
}

export function onDeviceList(message) {
  console.debug(message)
}

export function onPlayRT(data) {
  console.debug("onPlayRT" + data);
  if (data.error != "success") {
    this.$message({message: '播放失败', type: 'warning'});
  }
}

export function onDeviceVideoData(data, deviceId) {
  //设备视频流回调
  console.debug("onDeviceVideoData" + data);
}

export function onNotify(data) {
  console.log(data)
  let params = data.params;
  if (params.code == "DeviceStatus") {
    let deviceId = params.deviceId;
    if (params.action == "Offline" || params.action == "Normal") {
      removeCallList(deviceId)
    } else if (params.action == "Start") {
    } else if (params.action == "Dealing") {
      removeCallList(deviceId)
    }
    if (params.action == "Start" && !isHanding) {
      notifyWeb.onPlayMusic()
    } else {
      notifyWeb.onStopMusic()
    }
  }
}

export function handing() {
  isHanding = true
}

export function handled() {
  isHanding = false
  if (callingList.length > 0) {
    notifyWeb.onPlayMusic()
  }
}

export function fillZero(str) {
  let realNum;
  if (str < 10) {
    realNum = '0' + str;
  } else {
    realNum = str;
  }
  return realNum;
}

export function getTime() {
  const myDate = new Date();
  const myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
  const myMonth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
  const myToday = myDate.getDate(); //获取当前日(1-31)
  const myHour = myDate.getHours(); //获取当前小时数(0-23)
  const myMinute = myDate.getMinutes(); //获取当前分钟数(0-59)
  const mySecond = myDate.getSeconds(); //获取当前秒数(0-59)
  return myYear + '-' + fillZero(myMonth) + '-' + fillZero(myToday) + ' ' + fillZero(myHour) + ':' + fillZero(myMinute) + ':' + fillZero(mySecond);
}

